package com.carboncntr.struts.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.carboncntr.helper.HistoryHelper;
import com.carboncntr.helper.LookupHelper;
import com.carboncntr.struts.form.IndexForm;
import com.carboncntr.tables.Customer;
import com.carboncntr.tables.Lookup;

public class IndexAction extends DispatchAction {

	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		List<Lookup> list = LookupHelper.listlookup();
		IndexForm indexForm = (IndexForm)form;
		if(list != null) {
			indexForm.setLookupList(list);
		}
		return mapping.findForward("HOME");
	}
	
	public ActionForward submit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = (HttpSession)request.getSession();
		IndexForm indexForm = (IndexForm)form;
		BigDecimal carbonAmt = new BigDecimal(indexForm.getCarbonAmt());
		Customer c = (Customer)session.getAttribute("Customer");
		HistoryHelper.saveHistory(carbonAmt, c, new Date(), new Date());
		return mapping.findForward("init");
	}

}
